/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmBuscarClienteVentas.java
 *
 * Created on 19/11/2011, 04:08:00 PM
 */

package Ventas;

import Beans.ClienteBean;
import Blogic.ClienteBLogic;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author orellana
 */
public class frmBuscarClienteVentas extends javax.swing.JFrame {

    // Atributos de la Clase
    // Para la Tabla de Lista Clientes
    private String colSuc[]={"Codigo","Nombre Cliente","idCliente"};
    public DefaultTableModel modelDetalleClientes=new DefaultTableModel(colSuc,0);
    private String clienteSel="-1";
    private String nombreCliente="-1";
    private String codigoCliente="-1";

    /** Creates new form frmBuscarClienteVentas */
    public frmBuscarClienteVentas() {
        initComponents();
        // Definimos posicion del formulario
        this.setLocationRelativeTo(null);
        // Definimos el Redimensionamiento
        this.setResizable(false);
        // Definimos cerrado del formulario
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
        // Definimos el Tamaño de las Columnas (Lista Clientes)
        getTablaListaClientes();
    }

    // Metodos Publicos
    // Obtenemos el Id del Cliente
    public String getIdClienteSel(){
        // Retornamos el Valor
        return this.clienteSel;
    }

    // Obtenemos el Nombre del Cliente
    public String getNombreCliente(){
        // Retornamos el Valor
        return this.nombreCliente;
    }

    // Obtenemos el Codigo del Cliente
    public String getCodigoCliente(){
        // Retornamos el Valor
        return this.codigoCliente;
    }

    // Limpiamos el Formulario
    public void limpiaCampos(){
        // Codigo Cliente
        txtCodigoCliente.setText("");
        // Nombre Cliente
        txtNombreCliente.setText("");
        // Tabla de Clientes
        deleteRowModelTable();
    }

    // Metodos Privados
    // Obtenemos el Tamaño de las Columnas (Lista Clientes)
    private void getTablaListaClientes(){
        // Vaciamos el Modelo con los Datos
        tblListaClientes.setModel(modelDetalleClientes);
        // Definimos el Tamaño de las Columnas
        // Codigo
        tblListaClientes.getColumnModel().getColumn(0).setPreferredWidth(80);
        // Nombre
        tblListaClientes.getColumnModel().getColumn(1).setPreferredWidth(300);
        // idCliente
        tblListaClientes.getColumnModel().getColumn(2).setMinWidth(0);
        tblListaClientes.getColumnModel().getColumn(2).setMaxWidth(0);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        btnBuscar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtCodigoCliente = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtNombreCliente = new javax.swing.JTextField();
        btnAceptar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblListaClientes = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sistemapegasso.SistemaPegassoApp.class).getContext().getResourceMap(frmBuscarClienteVentas.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        btnBuscar.setIcon(resourceMap.getIcon("btnBuscar.icon")); // NOI18N
        btnBuscar.setText(resourceMap.getString("btnBuscar.text")); // NOI18N
        btnBuscar.setToolTipText(resourceMap.getString("btnBuscar.toolTipText")); // NOI18N
        btnBuscar.setBorderPainted(false);
        btnBuscar.setContentAreaFilled(false);
        btnBuscar.setFocusable(false);
        btnBuscar.setName("btnBuscar"); // NOI18N
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnBuscar);

        btnSalir.setIcon(resourceMap.getIcon("btnSalir.icon")); // NOI18N
        btnSalir.setText(resourceMap.getString("btnSalir.text")); // NOI18N
        btnSalir.setToolTipText(resourceMap.getString("btnSalir.toolTipText")); // NOI18N
        btnSalir.setBorderPainted(false);
        btnSalir.setContentAreaFilled(false);
        btnSalir.setFocusable(false);
        btnSalir.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSalir.setName("btnSalir"); // NOI18N
        btnSalir.setPreferredSize(new java.awt.Dimension(38, 38));
        btnSalir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSalir);

        jTabbedPane1.setFont(resourceMap.getFont("jTabbedPane1.font")); // NOI18N
        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setBackground(resourceMap.getColor("jLabel1.background")); // NOI18N
        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setForeground(resourceMap.getColor("jLabel1.foreground")); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jLabel1.setOpaque(true);

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txtCodigoCliente.setFont(resourceMap.getFont("txtCodigoCliente.font")); // NOI18N
        txtCodigoCliente.setText(resourceMap.getString("txtCodigoCliente.text")); // NOI18N
        txtCodigoCliente.setName("txtCodigoCliente"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtNombreCliente.setFont(resourceMap.getFont("txtNombreCliente.font")); // NOI18N
        txtNombreCliente.setName("txtNombreCliente"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCodigoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNombreCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCodigoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(8, 8, 8)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombreCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(25, 25, 25))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        btnAceptar.setBackground(resourceMap.getColor("btnAceptar.background")); // NOI18N
        btnAceptar.setFont(resourceMap.getFont("btnAceptar.font")); // NOI18N
        btnAceptar.setIcon(resourceMap.getIcon("btnAceptar.icon")); // NOI18N
        btnAceptar.setText(resourceMap.getString("btnAceptar.text")); // NOI18N
        btnAceptar.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btnAceptar.setFocusable(false);
        btnAceptar.setName("btnAceptar"); // NOI18N
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblListaClientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tblListaClientes.setName("tblListaClientes"); // NOI18N
        tblListaClientes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblListaClientesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblListaClientes);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 460, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 439, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 438, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(158, 158, 158)
                .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
        // Cerramos el formulario
        this.setVisible(false);
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        // TODO add your handling code here:
        // Variables Locales
        ClienteBean sqlBean=new ClienteBean();
        Vector clientes=new Vector();

        // Realizamos la Busqueda de Clientes
        // Seteamos los Datos
        sqlBean.setCodigoCliente(txtCodigoCliente.getText());
        sqlBean.setNombreCliente(txtNombreCliente.getText());
        // Ejecutamos la Consulta
        clientes=(new ClienteBLogic()).getDatosClientesByCodigoNombre(sqlBean);
        // Limpiamos la Tabla
        deleteRowModelTable();
        // Evaluamos el Resultado Obtenido
        if (clientes.size()!=0){
            // Ciclo FOR
            for (int i=0;i<clientes.size();i++){
                ClienteBean datoClientes=new ClienteBean();
                datoClientes=(ClienteBean) clientes.elementAt(i);
                // Vaciamos los Datos
                modelDetalleClientes.addRow(new Object[]{datoClientes.getCodigoCliente(),datoClientes.getNombreCliente(),datoClientes.getIdCliente()});
            }
        }else{
            // Vaciamos Registro No Existen Clientes Registrados
            modelDetalleClientes.addRow(new Object[]{"No existen Clientes Registrados"});
        }   
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void tblListaClientesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblListaClientesMouseClicked
        // TODO add your handling code here:
        this.clienteSel=tblListaClientes.getValueAt(tblListaClientes.getSelectedRow(),2).toString();
        this.nombreCliente=tblListaClientes.getValueAt(tblListaClientes.getSelectedRow(),1).toString();
        this.codigoCliente=tblListaClientes.getValueAt(tblListaClientes.getSelectedRow(),0).toString();
    }//GEN-LAST:event_tblListaClientesMouseClicked

    private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed
        // TODO add your handling code here:
        // Evaluamos si Selecciono un Cliente
        if (!clienteSel.equalsIgnoreCase("-1")){
            // Reseteamos la Variable de Seleccion
            clienteSel="-1";
            // Reseteamos la Variable de Nombre
            nombreCliente="-1";
            // Reseteamos la Variable de Codigo de Cliente
            codigoCliente="-1";
            // Cerramos el Formulario
            this.setVisible(false);
        }else{
            // Mensaje al Usuario
            JOptionPane.showMessageDialog(null,"Debe seleccionar un cliente, Verifique","Busqueda", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnAceptarActionPerformed

    private void deleteRowModelTable() {
        // Limpiamos el Modelo de la Tabla
        while (modelDetalleClientes.getRowCount() > 0) {
            modelDetalleClientes.removeRow(0);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable tblListaClientes;
    private javax.swing.JTextField txtCodigoCliente;
    private javax.swing.JTextField txtNombreCliente;
    // End of variables declaration//GEN-END:variables

}
